package com.applets.web.controller.jijin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.applets.common.utils.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的基金的最新净值
 *
 * @author lufeixia
 * @version 1.0
 * @date 2020-4-16 09:54:43
 * @since 1.8
 */
@Api(value = "/jijin", tags = "基金净值")
@RestController
@RequestMapping("/jijin")
public class ZuiXinJingZhi {

    public static void main(String[] args) throws InterruptedException {
        ZuiXinJingZhi zuiXinJingZhi = new ZuiXinJingZhi();
        // zuiXinJingZhi.test();
        // System.out.println(zuiXinJingZhi.jiJinXinXi("004753,050026,001595,009860,320007,002190,006060"));
        JSONArray csi1019 = null;

        System.out.println(csi1019);
    }

    @GetMapping("/jiJinXinXi/{code}")
    @ApiOperation(value = "基金信息")
    public JSONArray  jiJinXinXi(@PathVariable String code) {

        List<LinkedHashMap<String, String>> result = new ArrayList<>();

        String[] split = code.split(",");
        for (String code1 : split) {
            LinkedHashMap<String, String> result1 = new LinkedHashMap<>();
            result1.put("code", code1);

            String cookie = "";
            String url = "https://www.howbuy.com/fund/" + code1 + "/";
            JSONObject param = new JSONObject();
            String formResult = HttpUtil.httpForm(url, param, cookie);
            Document totalDoc = Jsoup.parse(formResult);
            result1.put("jz", totalDoc.getElementsByClass("dRate").text());

            String name = totalDoc.getElementsByTag("h1").text().replace("(", "-").split("-")[0];
            result1.put("name", name);

            String zhangfu = totalDoc.getElementsByClass("b-rate").get(0).text();
            result1.put("zhangfu", zhangfu);

            String url1 = "https://www.howbuy.com/fund/ajax/gmfund/valuation/valuationnav.htm?jjdm="+code1;
            String formResult1 = HttpUtil.httpForm(url1, param, cookie);
            Document totalDoc1 = Jsoup.parse(formResult1);
            result1.put("gz", totalDoc1.getElementsByClass("con_value").text());
            result.add(result1);
        }
        return JSONArray.parseArray(JSONArray.toJSONString(result));
    }

    @GetMapping("/jinZhi/{code}")
    @ApiOperation(value = "基金信息")
    public JSONArray jiJinJingZhi(@PathVariable String code) {
        JSONArray result = new JSONArray();

        String[] split = code.split(",");
        for (String code1 : split) {
            JSONObject result1 = new JSONObject();
            result1.put("code", code1);

            String cookie = "";
            String url = "https://www.howbuy.com/fund/" + code1 + "/";
            JSONObject param = new JSONObject();
            String formResult = HttpUtil.httpForm(url, param, cookie);
            Document totalDoc = Jsoup.parse(formResult);
            result1.put("value", totalDoc.getElementsByClass("dRate").text());
            result.add(result1);
        }
        return result;
    }

    @GetMapping("/guZhi/{code}")
    @ApiOperation(value = "基金信息")
    public JSONArray jiJinGuZhi(@PathVariable String code) {
        JSONArray result = new JSONArray();

        String[] split = code.split(",");
        for (String code1 : split) {
            JSONObject result1 = new JSONObject();
            result1.put("code", code1);

            String cookie = "";
            String url = "https://www.howbuy.com/fund/ajax/gmfund/valuation/valuationnav.htm?jjdm="+code1;
            JSONObject param = new JSONObject();
            String formResult1 = HttpUtil.httpForm(url, param, cookie);
            Document totalDoc1 = Jsoup.parse(formResult1);
            result1.put("value", totalDoc1.getElementsByClass("con_value").text());
            result.add(result1);
        }
        return result;
    }


    public void test() throws InterruptedException {
        ZuiXinJingZhi zuiXinJingZhi = new ZuiXinJingZhi();

        String[] codes = {"004753","050026","001595","009860","320007","002190","006060"};

        List<String> guzhi = new ArrayList<>();
        List<String> jingzhi = new ArrayList<>();
        for (int i = 0; i < codes.length; i++) {
            JSONObject result = zuiXinJingZhi.jiJinXinXiHouBy(codes[i]);
            System.out.println(result);
            jingzhi.add(result.getString("jingzhi"));
            guzhi.add(result.getString("guzhi"));
            Thread.sleep(100);
        }

        for (String s : jingzhi) {
            System.out.println(s);
        }
        System.out.println("----");
        for (String s : guzhi) {
            System.out.println(s);
        }

    }

    @GetMapping("/jiJinXinXiHouBy/{code}")
    @ApiOperation(value = "基金信息")
    public JSONObject jiJinXinXiHouBy(@PathVariable String code) {

        JSONObject result = new JSONObject();
        result.put("code", code);

        JSONObject param = new JSONObject();

        String cookie =
                "";
        String url = "https://www.howbuy.com/fund/" + code + "/";
        String formResult = HttpUtil.httpForm(url, param, cookie);

        Document totalDoc = Jsoup.parse(formResult);
        String name = totalDoc.getElementsByTag("h1").text().replace("(", "-").split("-")[0];
        result.put("name", name);

        String jingzhi = totalDoc.getElementsByClass("dRate").text();
        result.put("jingzhi", jingzhi);

        String date =
                totalDoc.getElementsByClass("b-0")
                        .get(0)
                        .text()
                        .replace("单位净值 [", "")
                        .replace("]", "");
        result.put("date", date);

        String zhangfu = totalDoc.getElementsByClass("b-rate").get(0).text();
        result.put("zhangfu", zhangfu);

        url = "https://www.howbuy.com/fund/ajax/gmfund/valuation/valuationnav.htm?jjdm="+code;
        String formResult1 = HttpUtil.httpForm(url, param, cookie);
        Document totalDoc1 = Jsoup.parse(formResult1);
        String guzhi = totalDoc1.getElementsByClass("con_value").text();
        result.put("guzhi", guzhi);
        String guzhiriqi = totalDoc1.getElementsByClass("tips_icon_con").text();
        result.put("guzhiriqi", guzhiriqi);

        return result;
    }


}
